#ifndef PARSER_ATTRIBUTESPARSER_H_
#define PARSER_ATTRIBUTESPARSER_H_

#include <fermat/parser/attributes.h>
#include <fermat/parser/tokenizer.h>
#include <string>

namespace fermat {
namespace parser {

/***********************************************************
* A parser for  C++ attributes.
*
* Using a special comment syntax, C++ declarations for
* structs/classes, functions, types, etc. can be annotated
* with attributes.
*
* Attributes always come immediately before the symbol that 
* is being annotated, and are written inside special comments
* with the syntax:
* @ <attrDecl>[,<attrDec>...]
* where <attrDecl> is
*     <name>[=<value>]
* <name> is a valid C++ identifier, or two identifiers separated by 
* a period (struct accessor notation).
* <value> is a string, integer, identifier, bool literal, or a complex value
* in the form
*    {<name>=<value>[,<name>=<value>...]}
*****************************************************************/
class  AttributesParser {
public:
	/*************************************************
	* Creates the AttributesParser.
	*************************************************/
	AttributesParser(Attributes& attrs, std::istream& istr);
	
	/*************************************************
	* Destroys the AttributesParser.
	*************************************************/
	~AttributesParser();
		
	/*************************************************
	* Parses attributes.
	*************************************************/
	void parse();
		

protected:
	void setAttribute(const std::string& name, const std::string& value);
	const fermat::Token* parseAttributes(const fermat::Token* pNext);
	const fermat::Token* parseAttribute(const fermat::Token* pNext);
	const fermat::Token* parseComplexAttribute(const Token* pNext, const std::string& id);
	const fermat::Token* parseIdentifier(const fermat::Token* pNext, std::string& id);
	const fermat::Token* next();
	static bool isIdentifier(const fermat::Token* pToken);
	static bool isOperator(const fermat::Token* pToken, int kind);
	static bool isLiteral(const fermat::Token* pToken);
	static bool isEOF(const fermat::Token* pToken);
	
private:
	Attributes  &_attrs;
	Tokenizer    _tokenizer;
	std::string  _id;
};


/****************************************
* inlines
*****************************************/
inline const fermat::Token* AttributesParser::next()
{
	return _tokenizer.next();
}


inline bool AttributesParser::isEOF(const fermat::Token* pToken)
{
	return pToken->is(Token::EOF_TOKEN);
}


inline bool AttributesParser::isIdentifier(const fermat::Token* pToken)
{
	return pToken->is(fermat::Token::IDENTIFIER_TOKEN) || pToken->is(fermat::Token::KEYWORD_TOKEN);
}


inline bool AttributesParser::isOperator(const fermat::Token* pToken, int kind)
{
	return pToken->is(fermat::Token::OPERATOR_TOKEN) && pToken->asInteger() == kind;
}


inline bool AttributesParser::isLiteral(const fermat::Token* pToken)
{
	return pToken->is(fermat::Token::STRING_LITERAL_TOKEN) || pToken->is(fermat::Token::INTEGER_LITERAL_TOKEN);
}


} 
} 

#endif 